package web.directories.action;

import com.opensymphony.xwork2.ActionSupport;
import domain.entities.ComiteEtica;
import domain.entities.IrbSop;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import services.CriteriaIrbSop;
import services.EthicsCommitteeCRUDService;
import services.IrbSopService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vgonzalez
 * Date: 05/11/12
 * Time: 16:02
 * To change this template use File | Settings | File Templates.
 */

@Namespace("/Directories/IrbSops")
public class IrbController extends ActionSupport {
    private EthicsCommitteeCRUDService ethicsCommitteeCRUDService;
    private IrbSopService irbSopService;

    private IrbSop irb = new IrbSop();
    private LinkedHashMap<String, ArrayList<ComiteEtica>> paisComites = new LinkedHashMap<String, ArrayList<ComiteEtica>>(0);
    private List<ComiteEtica> comites;
    private List<String> languages;
    private List<String> yesNo;
    private List<String> frequencies;
    private List<String> requiredTimelineList;
    private String countryIrb;

    private List<IrbSop> list;
    private Integer editableIrbId;

    @Autowired
    public IrbController(EthicsCommitteeCRUDService ethicsCommitteeCRUDService, IrbSopService irbSopService) {
        this.ethicsCommitteeCRUDService = ethicsCommitteeCRUDService;
        this.irbSopService = irbSopService;
        comites = ethicsCommitteeCRUDService.findAll();

        languages = new ArrayList<String>(0);
        languages.add("English");
        languages.add("Spanish");
        languages.add("Portuguese");

        yesNo = new ArrayList<String>(0);
        yesNo.add("Yes");
        yesNo.add("No");

        frequencies = new ArrayList<String>(0);
        frequencies.add("12 months");
        frequencies.add("24 months");

        requiredTimelineList = new ArrayList<String>(0);
        requiredTimelineList.add("1 day (working days)");
        requiredTimelineList.add("1 day (calendar)");
        requiredTimelineList.add("2 days (working days)");
        requiredTimelineList.add("2 days (calendar)");
        requiredTimelineList.add("3 days (working days)");
        requiredTimelineList.add("3 days (calendar)");
        requiredTimelineList.add("7 days (working days)");
        requiredTimelineList.add("7 days (calendar)");
        requiredTimelineList.add("15 days (working days)");
        requiredTimelineList.add("15 days (calendar)");
        requiredTimelineList.add("Each 3 months");
        requiredTimelineList.add("Each 6 months");
        requiredTimelineList.add("Other");
        requiredTimelineList.add("Not Specified");

        initPaisesComites(comites);
    }

    private void initPaisesComites(List<ComiteEtica> comites) {
        ArrayList<String> paises = new ArrayList<String>(0);
        for(ComiteEtica ce : comites){
            if(ce != null && ce.getAddress() != null && ce.getAddress().getCountry() != null
               &&!paises.contains(ce.getAddress().getCountry()))
                    paises.add(ce.getAddress().getCountry());
        }

        for(String pais : paises){
            ArrayList<ComiteEtica> comitesPorPais = new ArrayList<ComiteEtica>(0);
            for(ComiteEtica comiteEtica : comites){
                if(comiteEtica != null && comiteEtica.getAddress() != null && comiteEtica.getAddress().getCountry() != null
                   && comiteEtica.getAddress().getCountry().equals(pais)){
                        comitesPorPais.add(comiteEtica);
                }
            }
            paisComites.put(pais, comitesPorPais);
        }
    }


    @Action(value="main", results={
            @Result(name="success",location="/directories.irbsops.search.tiles", type="tiles")
    })
    public String execute(){
        return SUCCESS;
    }

    @Action(value="list", results={
            @Result(name="success",location="/directories.irbsops.search.tiles", type="tiles")
    })
    public String list(){
        CriteriaIrbSop crV = new CriteriaIrbSop(irb);
        list = irbSopService.list(crV);

        return SUCCESS;
    }

    @Action(value="create", results={
            @Result(name="success",location="/directories.irbsops.tiles", type="tiles")
    })
    public String create(){
        if(editableIrbId != null){
            irb = irbSopService.findById(editableIrbId);
        }else{
            Iterator it = comites.iterator();
            while(it.hasNext()){
                ComiteEtica next = (ComiteEtica) it.next();
                if(next.getSop() != null)
                    it.remove();
            }
        }
        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="/directories.irbsops.search.tiles", type="tiles")
    })
    public String save(){
        ComiteEtica comiteEtica = ethicsCommitteeCRUDService.findById(irb.getComiteEtica().getId());
        irb.setComiteEtica(comiteEtica);
        comiteEtica.setSop(irb);

        irbSopService.save(irb);

        irb = null;

        return SUCCESS;
    }

    @Action(value="getCommittees", results={
            @Result(name="success",location="/WEB-INF/jsp/Directories/irbsops/comitteeSelect.jsp")
    })
    public String getCommittees(){
        Iterator it = comites.iterator();
        while(it.hasNext()){
            ComiteEtica next = (ComiteEtica) it.next();
            if(next.getSop() != null)
                it.remove();
        }
        return SUCCESS;
    }

    public List<String> getYesNo() {
        return yesNo;
    }

    public void setYesNo(List<String> yesNo) {
        this.yesNo = yesNo;
    }

    public List<ComiteEtica> getComites() {
        return comites;
    }

    public void setComites(List<ComiteEtica> comites) {
        this.comites = comites;
    }

    public List<String> getLanguages() {
        return languages;
    }

    public void setLanguages(List<String> languages) {
        this.languages = languages;
    }

    public IrbSop getIrb() {
        return irb;
    }

    public void setIrb(IrbSop irb) {
        this.irb = irb;
    }

    public List<String> getFrequencies() {
        return frequencies;
    }

    public void setFrequencies(List<String> frequencies) {
        this.frequencies = frequencies;
    }

    public Integer getEditableIrbId() {
        return editableIrbId;
    }

    public void setEditableIrbId(Integer editableIrbId) {
        this.editableIrbId = editableIrbId;
    }

    public List<IrbSop> getList() {
        return list;
    }

    public void setList(List<IrbSop> list) {
        this.list = list;
    }

    public List<String> getRequiredTimelineList() {
        return requiredTimelineList;
    }

    public void setRequiredTimelineList(List<String> requiredTimelineList) {
        this.requiredTimelineList = requiredTimelineList;
    }

    public LinkedHashMap<String, ArrayList<ComiteEtica>> getPaisComites() {
        return paisComites;
    }

    public void setPaisComites(LinkedHashMap<String, ArrayList<ComiteEtica>> paisComites) {
        this.paisComites = paisComites;
    }

    public String getCountryIrb() {
        return countryIrb;
    }

    public void setCountryIrb(String countryIrb) {
        this.countryIrb = countryIrb;
    }
}
